<!doctype html>
<html>

<head>
	<meta charset="UTF-8">
	<meta name="copyright" content="www.apicloud.com" />
	<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
	<title>舆情工作平台登陆</title>
	<script type="text/javascript" src="./script/api.js"></script>
	<script type="text/javascript" src="./script/alpenlicebe.js"></script>
	<script type="text/javascript" src="./script/zepto.min.js"></script>
	<script type="text/javascript" src="./script/hashes.min.js"></script>
	<style>
		html {
			height: 100%;
		}

		body {
			height: 100%;
			width: 100%;
			background-image: url(./res/bg.png);
			background-size: 100%;
		}
	</style>
</head>

<body>
</body>
<script type="text/javascript">
	function goto_CheckInfoFun() {
		var deviceId_t = Alpe_getLocalStorge("deviceId");
		var systemType_t = Alpe_getLocalStorge("systemType");
		if ('' == deviceId_t || '' == systemType_t) {
			var systemType = api.systemType;
			if ("ios" == api.systemType) {
				systemType = "iphone";
			}
			var MD5 = new Hashes.MD5;
			var deviceId = MD5.hex(api.deviceId);

			Alpe_setLocalStorge('deviceId', deviceId);
			Alpe_setLocalStorge('systemType', systemType);
		}
	}
	apiready = function() {
		api.setStatusBarStyle({
			style: 'light',
			color: 'transparent'
		});
		goto_CheckInfoFun();
		if ("android" == api.systemType) {
			goto_open_permission("storage");
		}
		if ("HUAWEI" == api.deviceName) {
			goto_init_huaweipush();
			api.addEventListener({ //监听登录的时候写huaweitoken 用于后台推送消息
				name: 'Alpe_Event_UserLogin'
			}, function(ret) {
				if (ret) {
					goto_init_huaweipush();
				}
			});

		} else if ("Xiaomi" == api.deviceName) {
			goto_init_mipush();
		} else {
			// goto_init_ajpush();
			var ajpush = api.require('ajpush');
			//初始化推送服务
			ajpush.init(function(ret, err) {});
			setTimeout("goto_init_ajpush()",1000); ;
			goto_init_ajpush1();
		}

		if ('' != Alpe_getLocalStorge('sys_userID')) {
			var deviceId = Alpe_getLocalStorge('sys_deviceID');
			var userID = Alpe_getLocalStorge('sys_userID');
			if ('' == userID) {
				goto_openWin_loginWin("您未登录");
				// api.alert({
				// 	title: '系统提示',
				// 	msg: '您未登录'
				// });
				return;
			}
			var MD5 = new Hashes.MD5;
			var access_token = Alpe_getLocalStorge("access_token");
			var admin_id = Alpe_getLocalStorge("admin_id");
			var sign = MD5.hex(access_token + admin_id + api.deviceName + 'user/brand1.0syjqingbao');

			var url = Alpe_ApiUrl + 'r=user/brand&access_token=' + access_token + '&admin_id=' + admin_id + '&brand=' + api.deviceName + '&v=1.0&sign=' + sign;
			console.log(url);
			api.ajax({
				url: url,
				method: 'get',
				cache: false,
				timeout: 30,
				dataType: 'json',
				returnAll: false
			}, function(ret, err) {
				if (ret) {
					if ("101" != ret.success) {
						api.alert({
							title: '系统提示',
							msg: ret.message
						});
					}
				} else {
					api.alert({
						title: '系统提示',
						msg: '网络链接超时，请返回重试'
					});
					api.hideProgress();
				};
			});
		}


		api.openSlidLayout({
				type: 'left',
				leftEdge: 70,
				fixedPane: {
					name: 'main_Left',
					url: './html/main_Left.html',
					bgColor: '#353535',
					bounces: false,
					opaque: true,
					scaleEnabled: false,
					vScrollBarEnabled: true,
					hScrollBarEnabled: false
				},
				slidPane: {
					name: 'main_Main',
					url: './html/main_Main.html',
					bgColor: '#ccd8e2',
					bounces: false,
					opaque: true,
					vScrollBarEnabled: false,
					hScrollBarEnabled: false
				}
			},
			function(ret) {

			}
		);

		/*if(''==Alpe_getLocalStorge('sys_userID')){
  			var delay = 0;
		 	if("ios" != api.systemType){
		    	delay = 300;
		  	}
			api.openWin({
	            name: 'login',
	            url: './html/login.html',
	            bounces: false,
	            reload: true,
	            rect: {
	                x: 0,
	                y: 0,
	                w: 'auto',
	                h: 'auto'
	            },
	            delay: delay
	   		});

       	}*/
		/*************登陆页面子窗口***********************/
		/*************极光推送***********************/
		//绑定用户别名

	}


	function onResume() {
		var ajpush = api.require('ajpush');
		ajpush.onResume();
		console.log('JPush onResume');
	}

	/* 打开新闻详细 */
	function gotoNewsDetailFun_Push(extra) {
		var infoID = '';
		var my_key = '';
		var my_key = Alpe_getLocalStorge('sys_mykeyID_appintent');
		var infoID = Alpe_getLocalStorge('sys_pushmsgID_appintent');
		// if (api.systemType == "android") {
		// 	// try {
		// 		// 	var msg = eval("("+extra+")");
		// 		infoID = extra.news_id;
		// 		my_key = extra.my_key;
		// 	// } catch (er) {
		// 	// 	// 		alert($api.jsonToStr(er)+'a');
		// 	// }
		// } else {
		// 	// try {
		// 		infoID = extra.news_id;
		// 		my_key = extra.my_key;
		// 	// } catch (er) {
		// 	// 	// 		alert($api.jsonToStr(er)+'i');
		// 	// }
		// }
		if ('daily' == my_key) {
			api.openWin({
				name: 'list_mryq_top',
				url: "./html/list_mryq_top.html",
				pageParam: {
					infoID: infoID
				},
				bounces: false,
				vScrollBarEnabled: false,
				hScrollBarEnabled: false,
				reload: true
			});
		} else if ('hot' == my_key) {
			api.openWin({
				name: 'hot_TrackListInfoListDetail',
				url: "./html/hot_TrackListInfoListDetail.html",
				pageParam: {
					infoID: infoID
				},
				bounces: false,
				vScrollBarEnabled: false,
				hScrollBarEnabled: false,
				reload: true
			});
		} else if ('count' == my_key) {
			api.openWin({
				name: 'tjfx_Index',
				url: "./html/tjfx_Index.html",
				pageParam: {
					infoID: infoID
				},
				bounces: false,
				vScrollBarEnabled: false,
				hScrollBarEnabled: false,
				reload: true
			});
		} else {
			if ('' != infoID) {
				api.openWin({
					name: 'yq_listDetail',
					url: "./html/yq_listDetail.html",
					pageParam: {
						infoID: infoID
					},
					bounces: false,
					vScrollBarEnabled: false,
					hScrollBarEnabled: false,
					reload: true
				});
			}
		}
	}

	function goto_init_ajpush() {
		var ajpush = api.require('ajpush');
		//初始化推送服务
		// ajpush.init(function(ret, err) {});
		var deviceId = Alpe_getLocalStorge("deviceId");
		var param = {
			alias: deviceId,
			sequence:101
		};
		ajpush.setAlias(param, function(ret) {
			// alert(JSON.stringify(ret));
			console.log("bindAliasAndTags" + JSON.stringify(ret));
			var statusCode = ret.statusCode;
		});
		// ajpush.setListener(
		// 		function(ret) {
		// 			console.log(JSON.stringify(ret));
		// 			var my_key = ret.my_key;
		// 			var news_id = ret.news_id;
		// 			alert(my_key);
		// 			alert(news_id);
		// 			Alpe_setLocalStorge('sys_mykeyID_appintent', my_key);
		// 			Alpe_setLocalStorge('sys_pushmsgID_appintent', news_id);
		// 			setTimeout('gotoNewsDetailFun_Push()', 1000);
		// 		}
		// );

	}

	function goto_init_ajpush1() {
		//Android点击消息
		if (api.systemType == "android") {
			/*消息/通知点击时触发（android）*/
			api.addEventListener({
				name: 'appintent'
			}, function(ret, err) {
				// alert(JSON.stringify(ret));
				if (ret && ret.appParam.ajpush) {
					var ajpushMsg = ret.appParam.ajpush;
					var id = ajpushMsg.id;
					var title = ajpushMsg.content;
					var content = ajpushMsg.content;
					var extra = ajpushMsg.extra;
					Alpe_setLocalStorge('sys_mykeyID_appintent', extra.my_key);
					Alpe_setLocalStorge('sys_pushmsgID_appintent', extra.news_id);
					setTimeout('gotoNewsDetailFun_Push()', 1000);
					// gotoNewsDetailFun_Push(extra);
				}
			});
		}

		//ios点击消息
		if (api.systemType == "ios") {

			/*在iOS平台，当应用在后台时，使用极光推送发送通知时（消息只有应用在前台才能收到），系统会往设备发送通知。
			当通知被点击后，若应用已启动，则通过上面的setListener回调给开发者；若应用未启动，APICloud会将本次推送的内容通过事件监听回调的方式交给开发者*/
			api.addEventListener({
				name: 'noticeclicked'
			}, function(ret, err) {
				console.log(JSON.stringify(ret));
				if (ret && ret.my_key) {
					var my_key = ret.my_key;
					var news_id = ret.news_id;
					Alpe_setLocalStorge('sys_mykeyID_appintent', my_key);
					Alpe_setLocalStorge('sys_pushmsgID_appintent', news_id);
					setTimeout('gotoNewsDetailFun_Push()', 1000);
					// gotoNewsDetailFun_Push(extra);
				}
			});
		}

		//设置监听事件
		api.addEventListener({
			name: 'pause'
		}, function(ret, err) {
			onResume(); //监听应用进入后台，通知jpush暂停事件
		})
		api.addEventListener({
			name: 'resume'
		}, function(ret, err) {
			onResume(); //监听应用恢复到前台，通知jpush恢复事件
		})
	}

	function goto_init_huaweipush() {

		var huaweiPush = api.require('huaweiPush');

		//如监听网络连接事件
		api.addEventListener({
			name: 'appintent'
		}, function(ret, err) {
			// alert(JSON.stringify(ret));
			// alert(JSON.stringify(err));
			if (ret && ret.appParam.message) {
				var msg = ret.appParam.message;
				// alert(msg);
				var ext = JSON.parse(msg);
				// alert(ext);
				// gotoNewsDetailFun_Push(ext);
				Alpe_setLocalStorge('sys_mykeyID_appintent', ext.my_key);
				Alpe_setLocalStorge('sys_pushmsgID_appintent', ext.news_id);
				setTimeout('gotoNewsDetailFun_Push()', 1000);
				// gotoNewsDetailFun_Push(ext);
			}

		});
		huaweiPush.init(function(ret, err) {
			if (ret.status) {
				huaweiPush.getToken(function(ret1, err1) {
					console.log(JSON.stringify(ret1));
					console.log(JSON.stringify(err1));
					var deviceId = Alpe_getLocalStorge('sys_deviceID');
					var userID = Alpe_getLocalStorge('sys_userID');
					if ('' == userID) {
						goto_openWin_loginWin("您未登录");
						// api.alert({
						// 	title: '系统提示',
						// 	msg: '您未登录'
						// });
						return;
					}

					var MD5 = new Hashes.MD5;
					var access_token = Alpe_getLocalStorge("access_token");
					var admin_id = Alpe_getLocalStorge("admin_id");
					var sign = MD5.hex(access_token + admin_id + ret1.token + 'user/huaweitoken1.0syjqingbao');

					var url = Alpe_ApiUrl + 'r=user/huaweitoken&access_token=' + access_token + '&admin_id=' + admin_id + '&huaweitoken=' + ret1.token + '&v=1.0&sign=' + sign;

					// var URL = Alpe_ApiUrl+'?r=datas/huaweitoken&v='+api.version
					// 		+'&huaweitoken='+ret1.token
					// 		+'&userID='+userID+'&phoneID='+deviceId+'&phoneType='+api.systemType;
					console.log(url);
					if (ret.status) {
						api.ajax({
							url: url,
							method: 'get',
							cache: false,
							timeout: 30,
							dataType: 'json',
							returnAll: false
						}, function(ret, err) {
							if (ret) {
								if ("101" != ret.success) {
									api.alert({
										title: '系统提示',
										msg: ret.message
									});
								}
							} else {
								api.alert({
									title: '系统提示',
									msg: '网络链接超时，请返回重试'
								});
								api.hideProgress();
							};
						});
					} else {
						api.alert({
							msg: "获取token失败"
						});
					}
				});
			} else {
				api.alert({
					msg: "初始化失败"
				});
			}
		});

	}

	function goto_init_mipush() {
		api.addEventListener({
			name: 'appintent'
		}, function(ret, err) {
			// alert(JSON.stringify(ret));
			console.log(JSON.stringify(ret));
			var msg = ret.appParam.message;
			// alert(msg);
			var ext = JSON.parse(msg);
			Alpe_setLocalStorge('sys_mykeyID_appintent', ext.my_key);
			Alpe_setLocalStorge('sys_pushmsgID_appintent', ext.news_id);
			setTimeout('gotoNewsDetailFun_Push()', 1000);
			// gotoNewsDetailFun_Push(ext);
		});
		var mipush = api.require('mipush');
		mipush.registerPush({
			appId: '2882303761520017000',
			appKey: '5242001768000'
		}, function(ret) {
			console.log(JSON.stringify(ret));
			var deviceId = Alpe_getLocalStorge('deviceId');
			console.log(deviceId);
			mipush.setAlias({
				alias: deviceId,
			}, function(ret) {
				console.log(JSON.stringify(ret));
			});
		});
	}


	function goto_open_permission(perm) {
		// alert(perm);
		var perms = new Array();
		perms.push(perm);
		var has = api.hasPermission({
			list: perms
		});
		// alert(JSON.stringify(has));
		if (!has || !has[0] || !has[0].granted) {
			api.confirm({
				title: '提醒',
				msg: "没有开启存储权限,会导致部分功能异常\n是否前往设置?",
				buttons: ['去设置', '取消']
			}, function(ret, err) {
				if (1 == ret.buttonIndex) {
					api.requestPermission({
						list: perms,
						code: 100001
					}, function(ret, err) {
						var str = '请求结果：\n';
						str += '请求码: ' + ret.code + '\n';
						str += "是否勾选\"不再询问\"按钮: " + (ret.never ? '是' : '否') + '\n';
						str += '请求结果: \n';
						var list = ret.list;
						for (var i in list) {
							str += list[i].name + '=' + list[i].granted + '\n';
						}
						apialert(str);
						console.log(JSON.stringify(ret));
					});
				}
			});
			return false;
		}
		return true;
	}
</script>

</html>
